
package com.sfdlJPush.controller;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import com.sfdlJPush.R;

public abstract class MainFragment extends Fragment
{
    protected View containerView;
    public Context mContext;
    public Activity mActivity;
    protected ProgressBar mProgressBar;
    private Bundle bundle;

    public View onCreateView(LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState)
    {
        if (containerView == null)
        {
            containerView = inflater.inflate(doGetContentViewId(), container, false);
            doBefore(savedInstanceState);
            init(containerView);
            doInitSubViews(containerView);
            doInitDataes();
            doAfter();
        }
        ViewGroup parent = (ViewGroup) containerView.getParent();
        if (parent != null)
        {
            parent.removeView(containerView);
        }
        return containerView;
    }

    private void init(View containerView)
    {
        // TODO Auto-generated method stub
        mActivity = getActivity();
        mContext = getActivity();
        if (mActivity instanceof MainActivity)
        {
            mProgressBar = (ProgressBar) mActivity.findViewById(R.id.pb_head);
        }
    }

    public abstract int doGetContentViewId();

    public abstract void doInitSubViews(View containerView);

    public abstract void doInitDataes();

    public abstract void doAfter();

    private void doBefore(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
    }

    public Bundle getBundle()
    {
        return bundle;
    }

    public void setBundle(Bundle bundle)
    {
        this.bundle = bundle;
    }

    public void replaceFragment(Class<?> clazz)
    {
        replaceFragment(clazz, null, false);
    }

    public void replaceFragment(Class<?> clazz , Bundle bundle , boolean isFirst)
    {
        ((MainActivity) mActivity).replaceFragment(clazz, bundle, isFirst);
    }

    @Override
    public void onResume()
    {
        // TODO Auto-generated method stub
        super.onResume();
        ((MainActivity) mActivity).onFragmentResume(this);
    }
}
